<?php
namespace app\admin\controller;
use think\Request;

class Wdgs extends Base
{
    
    // 首页框架
    public function index()
    {
        if ( ! session('?user_id')) {
            $this->redirect('admin/wdgs/login');
        }
        
        return $this->fetch();
    }
    
    // 登录
    public function login()
    {
        if(request()->isPost()) {

            $post = request()->only(['account', 'pwd'], 'post');

            if (empty($post['account'])) {
                err('请输入账号');
            }
            if (empty($post['pwd'])) {
                err('请输入密码');
            }
            
            $result = db('customer')->where(['username' => $post['account']])
                ->field("id, mobile, username, name, pwd, status, expire, rules")
                ->find();

            // 查找不到
            if(! $result) {
                err('该账号不存在');
            }
            
            if($result['status'] != 1) {
                err('账号已禁用，请联系管理员');
            }
 
            // 密码不正确
            if(md5($post['pwd']) != $result['pwd']) {
                err('密码错误，请重新输入');
            }

            $rules = explode(',', $result['rules']);
            session('user_id', $result['id']);
            session("user_account", $result['username']);
            session('user_name', $result['name']);
            session('user_auth', $rules);

            // 验证成功
            suc('验证成功');
        }
        
        // 默认登录失败
        return $this->fetch();
    }
    
    // 登出
    public function logout()
    {
        session('user_id', null);
        session('user_account', null);
        session('user_name', null);
        session('user_auth', null);
        return $this->redirect('admin/wdgs/login');
    }
    
    // 欢迎页面
    public function welcome()
    {
        return $this->fetch();
    }
   
    
    //会员列表
    public function user_list()
    {
        $post = request()->only(['number'],'post');
        
        if (!empty($post['number'])){
            $where['a.distance'] = ['gt',$post['number']];
        }
        
        $field = "a.id,a.user_id,a.distance,a.game_list_number,a.share_number,a.game_wudi_use,a.game_number,b.avatarurl,b.nickname";
        $where['a.admin_id'] = $this->accountId;
        $where['b.nickname'] = ['neq',''];
        $where['b.avatarurl'] = ['neq',''];
        $res = db('wdgs_center')->alias('a')->join('user b','a.user_id = b.id','left')->field($field)
                ->where($where)->order('b.create_time desc')->paginate($this->page, false);
        
        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res', $res);
        return $this->fetch();
        
    }
    
    //修改页面
    public function user_list_edit($id)
    {
        $res = db('wdgs_center')->where(['id'=>$id])->field('id,game_number,game_wudi_number')->find();

        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //修改挑战次数
    public function user_edit_post()
    {
        $post = request()->only(['id','game_number','game_wudi_number'],'post');
        
        $res = db('wdgs_center')->where(['id'=>$post['id']])->update(['game_number'=>$post['game_number'],'game_wudi_number'=>$post['game_wudi_number']]);
        
        if ($res) {
            $this->success('编辑成功','wdgs/user_list');
        }else{
            $this->error('编辑失败');
        }
    }
    
    //配置管理
    public function configure()
    {
        $res = db('wdgs_config')->where(['admin_id'=>$this->accountId])->find();
        
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //编辑配置
    public function configure_post()
    {
        $post = request()->only(['share_humber','share_number','share_max','share_title','game_number_day','game_number_new','game_over',
                                'game_wuhui_number','game_wudi','game_time','game_off','game_uid','game_over_one','game_over_two',
                                'game_over_therr'
        ],'post');
        
        //图片上传
        $file = new \app\admin\logic\File();
        $image = $file->uploadFileAdmin($this->accountId);
        
        if (!empty($image)){
            $post['code'] = $image;
        }
        
        $res = db('wdgs_config')->where(['admin_id'=>$this->accountId])->update($post);
        
        if ($res) {
            $this->success('编辑成功','wdgs/configure');
        }else{
            $this->error('编辑失败');
        }
    }
    
    
}